package sqlAndIo.util;

import java.sql.Connection;
import java.util.Date;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;

public class DBUtil {
	private static String url = "jdbc:oracle:thin:@ZHA-ITA009-W7:1521:orcl";
	private static String driver = "oracle.jdbc.driver.OracleDriver";//oracle.jdbc.driver.OracleDriver
	private static String username = "ariel";
	private static String password = "ariel";
	
	public static Connection getConnection(){
		Connection connection = null;
		try {
			Class.forName(driver);
			connection = DriverManager.getConnection(url, username, password);
		} catch (SQLException e) {
			e.printStackTrace();
		} 
		catch (ClassNotFoundException e) {
			e.printStackTrace();
		} 
		return connection;
	}
	
	public static Date strToDate(String dateStr,String pattern)throws Exception{
		DateFormat format=new SimpleDateFormat(pattern);
		
		return format.parse(dateStr);
		
	}
	
	public static String  dateToStr(Date date,String pattern)throws Exception{
		DateFormat format=new SimpleDateFormat(pattern);
		
		return format.format(date);
		
	}
	public static void close(ResultSet rs,PreparedStatement ps,Connection conn){
		try {
			if(rs!=null){
				rs.close();
			}
			if(ps!=null){
				ps.close();
			}
			if(conn!=null){
				conn.close();
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
}
